# -*- coding:utf-8 -*-
#作者：琴弦上的宇宙
#时间：2021年10月22日

import time
from common import *
from openCv import *
from myGui import *

#基础
from base.scStoneStart import *
from base.scStoneInsure import *
from base.scModeChoose import *

#佣兵
from mercenary.base.scPointChoose import *
from mercenary.base.scSelectCard import *
from mercenary.base.scFightBox import *
from mercenary.base.scTaskFinish import *
from mercenary.base.scSingleHand import *

#佣兵PVE
from mercenary.pve.scPveSelectZone import *
from mercenary.pve.scPveSelectLevel import *
from mercenary.pve.scPveFightChoose import *
from mercenary.pve.scPveFightIng import *
from mercenary.pve.scPveSelectTreasury import *
from mercenary.pve.scPveSelectSurprise import *
from mercenary.pve.scPveFightQuit import *
from mercenary.pve.scPveFightFinish import *

def threadAutoAi():
    ai=AutoAi()
    ai.run()

class AutoAi(object):
    def __init__(self):
        self.mode=None
        self.bNeedReset=False
        self.noMatchCnt=0
        self.scAll=[]

    def setMode(self,mode):
        self.scAll.clear()
        if mode=='modePve':
            self.scAll.append(scStoneStart(True))
            self.scAll.append(scStoneInsure(True))
            self.scAll.append(scModeChoose(True))
            self.scAll.append(scPointChoose(True))
            self.scAll.append(scSelectCard(True))
            self.scAll.append(scPveSelectZone(True))
            self.scAll.append(scPveSelectLevel(True))
            self.scAll.append(scPveFightChoose(True))
            self.scAll.append(scPveFightIng(True))
            self.scAll.append(scPveSelectTreasury(True))
            self.scAll.append(scPveSelectSurprise(True))
            self.scAll.append(scPveFightQuit(True))
            self.scAll.append(scPveFightFinish(True))
            self.scAll.append(scFightBox(True))
            self.scAll.append(scTaskFinish(True))
            self.scAll.append(scSingleHand(True))

    def disAllScene(self):
        for sc in self.scAll:
            if sc.bValid:
                sc.dis()

    def enableAllScene(self):
        for sc in self.scAll:
            if not sc.bValid:
                sc.enable()

    def enableScene(self,name):
        for sc in self.scAll:
            if name in sc.name:
                sc.enable()
                break

    def setSingleHandFunc(self,funcNameList):
        for sc in self.scAll:
            if 'scSingleHand' in sc.name:
                sc.setFunc(funcNameList)
                sc.enable()
                break

    def procScene(self):
        background=SaveScreen()
        for sc in self.scAll:
            if sc.isOwn(background) and sc.proc(background):
                self.noMatchCnt=0
                if sc.name=='scStoneStart':#启动炉石后
                    self.disAllScene()
                    self.enableScene('scStoneStart')
                    self.enableScene('scStoneInsure')
                    return
                elif sc.name=='scStoneInsure':#确认提示后
                    self.disAllScene()
                    self.enableScene('scStoneInsure')
                    self.enableScene('scModeChoose')
                    return
                elif sc.name=='scModeChoose':#选择佣兵战纪后
                    self.disAllScene()
                    self.enableScene('scModeChoose')
                    self.enableScene('scPointChoose')
                    return
                elif sc.name=='scPointChoose':#选择PVE/PVP后
                    self.disAllScene()
                    self.enableScene('scPointChoose')
                    self.enableScene('scPveSelectZone')
                    return
                elif sc.name=='scPveSelectZone':#选择副本1/2/3/4大关卡后
                    self.disAllScene()
                    self.enableScene('scPveSelectZone')
                    self.enableScene('scPveSelectLevel')
                    return
                elif sc.name=='scPveSelectLevel':#选择副本1-9小关卡后
                    self.disAllScene()
                    self.enableScene('scPveSelectLevel')
                    self.enableScene('scSelectCard')
                    self.enableScene('scPointChoose')
                    return
                elif sc.name=='scSelectCard':#选择卡组后
                    self.disAllScene()
                    self.enableScene('scSelectCard')
                    self.enableScene('scPveFightChoose')
                    return

                #战斗选择
                elif sc.name=='scPveFightChoose_StartInter':#战斗开始后
                    self.disAllScene()
                    self.enableScene('scPveFightIng')
                    return
                elif sc.name=='scPveFightChoose_StartGoto':#选择前往后，有复活和神秘人
                    self.disAllScene()
                    self.enableScene('scPveFightChoose')
                    self.enableScene('scPveSelectSurprise')
                    return
                elif sc.name=='scPveFightChoose_StartSkip':#跳跃即直接打boss后
                    self.disAllScene()
                    self.enableScene('scPveFightChoose')
                    return
                elif sc.name=='scPveFightChoose_StartShow' or\
                    sc.name=='scPveFightChoose_StartGet':#选择战斗buff后
                    self.disAllScene()
                    self.enableScene('scPveFightChoose')
                    self.setSingleHandFunc(['FightBuff'])
                    return
                elif sc.name=='scPveFightChoose_TeamView':#选择团队查看即放弃后
                    self.disAllScene()
                    self.enableScene('scPveFightChoose')
                    self.enableScene('scPveFightQuit')
                    return
                elif sc.name=='scPveFightChoose_Task':#选择任务查看后
                    self.disAllScene()
                    self.enableScene('scPveFightChoose')
                    self.enableScene('scTaskFinish')
                    return

                #单个
                elif sc.name=='scSingleHand_FightRound' or\
                    sc.name=='scSingleHand_HeroUp' or\
                    sc.name=='scSingleHand_HeroNewSkill':#战斗过小关和英雄升级，英雄新技能后
                    self.disAllScene()
                    self.enableScene('scPveSelectTreasury')
                    self.setSingleHandFunc([])
                    self.enableScene('scFightBox')
                    self.enableScene('scPveFightChoose')
                    self.enableScene('scPveSelectLevel')
                    return
                elif sc.name=='scSingleHand_SurpriseNewTask':#选择神秘人新任务后
                    self.disAllScene()
                    self.setSingleHandFunc([])
                    self.enableScene('scPveFightChoose')
                    return
                elif sc.name=='scSingleHand_FightBuff' or\
                    sc.name=='scSingleHand_SurpriseAward':#选择战斗buff和神秘人卡硬币即硬币安慰奖后
                    self.disAllScene()
                    self.setSingleHandFunc([])
                    self.enableScene('scPveFightChoose')
                    return
                elif sc.name=='scSingleHand_GetTaskAward':#任务完成选择领取奖励后
                    self.disAllScene()
                    self.setSingleHandFunc([])
                    return
                elif sc.name=='scSingleHand_FightQuitAward':#选择战利品即放弃副本或失败后
                    self.disAllScene()
                    self.setSingleHandFunc([])
                    self.enableScene('scPveSelectLevel')
                    return
                elif sc.name=='scSingleHand_HeroChip' or\
                    sc.name=='scSingleHand_HeroNewEquip':#选择任务奖励即英雄碎片和英雄新装备后
                    self.disAllScene()
                    self.setSingleHandFunc([])
                    self.enableScene('scTaskFinish')
                    return

                #任务
                elif sc.name=='scTaskFinish_UnFinished':#任务未完成后
                    self.disAllScene()
                    self.enableScene('scTaskFinish')
                    self.enableScene('scPveFightChoose')
                    return
                elif sc.name=='scTaskFinish_Task':#任务5/7等
                    self.disAllScene()
                    self.enableScene('scTaskFinish')
                    self.enableScene('scPveFightChoose')
                    return
                elif sc.name=='scTaskFinish_Finished':#任务已完成后
                    self.disAllScene()
                    self.enableScene('scTaskFinish')
                    self.setSingleHandFunc(['GetTaskAward','HeroNewEquip'])
                    return

                elif sc.name=='scPveFightIng':#正在战斗中
                    self.disAllScene()
                    self.enableScene('scPveFightIng')
                    self.setSingleHandFunc(['FightRound'])
                    return
                elif sc.name=='scPveSelectTreasury':#选择宝藏后
                    self.disAllScene()
                    self.enableScene('scPveSelectTreasury')
                    self.enableScene('scPveFightChoose')
                    return
                elif sc.name=='scPveSelectSurprise':#选择神秘人后，有硬币安慰奖和新任务
                    self.disAllScene()
                    self.enableScene('scPveSelectSurprise')
                    self.setSingleHandFunc(['SurpriseAward','SurpriseNewTask'])
                    return
                elif sc.name=='scPveFightQuit':#选择放弃后，有战利品
                    self.disAllScene()
                    self.enableScene('scPveFightQuit')
                    self.enableScene('scPveSelectLevel')
                    self.setSingleHandFunc(['FightQuitAward'])
                    return
                elif sc.name=='scFightBox':#开宝箱后
                    self.disAllScene()
                    self.enableScene('scFightBox')
                    self.enableScene('scPveFightFinish')
                    return
                elif sc.name=='scPveFightFinish':#副本完成后
                    self.disAllScene()
                    self.enableScene('scPveFightFinish')
                    self.enableScene('scPveSelectLevel')
                    return
            else:
                self.noMatchCnt+=1
                if self.noMatchCnt>1000:
                    SaveErrorPng()
                    self.bNeedReset=True
                    self.noMatchCnt=0
                    break
        if self.bNeedReset:
            self.enableAllScene()
            self.setSingleHandFunc([])

    def run(self):
        while True:
            MyGui.gWait=0.1
            time.sleep(0.1)
            if MyGui.bAutoAi:
                if MyGui.bResetScene:
                    if MyGui.gGameMode=='1':
                        self.setMode('modePve')
                    MyGui.bResetScene=False
                self.procScene()
